<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\restaurant\server;

use app\restaurant\model\OrderAddress;

use app\restaurant\model\Store;

use app\restaurant\model\Order;

use app\restaurant\model\StoreConfig;
use app\restaurant\model\TransporterOrder;


class Context{


    protected $calss;


    /**
     * Context constructor.
     * @param $calss
     *
     */
    public function __construct($id='',$uniacid='',$store_id='')
    {

        //如果传了id，下单时候选择的时候就是什么
        if(!empty($id)){

            $transporter_model = new TransporterOrder();

            $type = $transporter_model->where(['order_code'=>$id])->value('type');

            $order_model = new Order();

            $store_id = $order_model->where(['order_code'=>$id])->value('store_id');

            $uniacid  = $order_model->where(['order_code'=>$id])->value('uniacid');

        }else{
            //如果没有就是配置的
            $store_config_model = new StoreConfig();

            $config = $store_config_model->configInfo(['uniacid'=>$uniacid,'store_id'=>$store_id]);

            $type   = $config['send_type'];
        }


        //达达平台
        if($type==1){

            $calss = new DadaApi($uniacid,$store_id);
            //商家配送
        }elseif ($type==0){

            $calss = new StoreApi($uniacid,$store_id);


        }

        $this->calss = $calss;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-29 14:18
     * @功能说明:获取三方订单配送费
     */
    public function getDeliverFee($id){

        $order = $this->calss->getDeliverFee($id);

        return $order;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 10:52
     * @功能说明:商家三方发单
     */
    public function addOrder($order_id){

        $res =  $this->calss->addOrder($order_id);

        return $res;

    }

    /**
     * @param $order_id
     * @功能说明:商家重新发单(如果三方平台异常或特殊原因商家需要重新发单)
     * @author chenniang
     * @DataTime: 2020-10-30 19:24
     */
    public function reAddOrder($order_id){

        $res =  $this->calss->reAddOrder($order_id);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 10:52
     * @功能说明:查询三方平台订单
     */
    public function orderInfo($order_id){

        $res =  $this->calss->orderInfo($order_id);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 17:39
     * @功能说明:主要检测三方平台的账号密码是否正确
     */
    public function check($store_id = ''){


        $res =  $this->calss->check($store_id);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-10-30 10:52
     * @功能说明:取消订单
     */
    public function orderCancel($order_id,$cancel_reason_id=4,$cancel_reason=''){

        $res =  $this->calss->orderCancel($order_id,$cancel_reason_id,$cancel_reason);

        return $res;

    }


}